#include "../head/server.h"

int remove_work(int net_fd, const char *username, MYSQL *mysql)
{
	train_t train;
	bzero(&train, sizeof(train));
	recv(net_fd, &train.length, sizeof(train.length), MSG_WAITALL);
	recv(net_fd, train.data, train.length, MSG_WAITALL);

	// 用户输入的任意路径
	char path[1024] = {0};
	memcpy(path, train.data, train.length);
	// puts(path);

	// 从数据库中拿到当前路径
	char *cur_path = get_current_path_by_username(mysql, username);

	// 获取绝对路径
	char absolutePath[1024] = {0};
	if (!memcmp(path, "/", strlen("/")))
	{
		memcpy(absolutePath, path, strlen(path));
	}
	else
	{
		memcpy(absolutePath, cur_path, strlen(cur_path));
		strcat(absolutePath, "/");
		strcat(absolutePath, path);
	}
	getAbsolutePath(absolutePath);
	puts(absolutePath);

	if (is_path(mysql, absolutePath))
	{
		char all_path[1024] = {0};
		getparameter("root_path", all_path);
		strcat(all_path, absolutePath);
		char sql[1024] = {0};
		sprintf(sql, "select count(*) from all_user_file where hash_value = (select distinct hash_value from all_user_file where file_path = '%s' );", absolutePath);
		int qret = mysql_query(mysql, sql);
		if (qret)
		{
			printf("%s\n", mysql_error(mysql));
			return 1;
		}
		MYSQL_RES *res = mysql_store_result(mysql);
		if (res == NULL)
		{
			printf("%s\n", mysql_error(mysql));
			return 1;
		}
		MYSQL_ROW row = mysql_fetch_row(res);
		if (row == NULL)
		{
			printf("%s\n", mysql_error(mysql));
			return 1;
		}
		if (atoi(row[0]) > 1)
		{
			bzero(&sql, sizeof(sql));
			sprintf(sql, "delete from all_user_file where username = '%s' and file_path = '%s';", username, absolutePath);
			if (mysql_query(mysql, sql))
			{
				printf("表中文件信息删除失败！错误：%s\n", mysql_error(mysql));
				return 1;
			}
			printf("表中文件信息删除成功！\n");
			bzero(&train, sizeof(train));
			train.length = strlen("success");
			memcpy(train.data, "success", train.length);
			send(net_fd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
			return 0;
		}
		// 接收到绝对路径后，删除操作
		if (remove(all_path) == 0)
		{
			printf("文件删除成功。");

			// 然后，删除表中文件的所在行
			char sql_del[1024] = {0};

			sprintf(sql_del, "delete from file_table where hash_value = (select hash_value from all_user_file where username = '%s' and file_path = '%s');", username, absolutePath);
			if (mysql_query(mysql, sql_del))
			{
				printf("表中文件信息删除失败！错误：%s\n", mysql_error(mysql));
				return 1;
			}

			bzero(&sql_del, sizeof(sql_del));
			sprintf(sql_del, "delete from all_user_file where username = '%s' and file_path = '%s';", username, absolutePath);
			if (mysql_query(mysql, sql_del))
			{
				printf("表中文件信息删除失败！错误：%s\n", mysql_error(mysql));
				return 1;
			}

			printf("表中文件信息删除成功！\n");
			bzero(&train, sizeof(train));
			train.length = strlen("success");
			memcpy(train.data, "success", train.length);
			send(net_fd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);

			puts(sql_del);
		}
		else
		{
			bzero(&train, sizeof(train));
			train.length = strlen("delete_fail");
			memcpy(train.data, "delete_fail", train.length);
			send(net_fd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
			printf("删除失败！\n");
		}
	}
	else
	{
		bzero(&train, sizeof(train));
		train.length = strlen("query_fail");
		memcpy(train.data, "query_fail", train.length);
		send(net_fd, &train, sizeof(train.length) + train.length, MSG_NOSIGNAL);
	}

	// 释放内存
	free(cur_path);
	return 0;
}